package stack.era.presentation;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JLabel;

public abstract class ERAInfoBar extends JLabel {

	private static final long serialVersionUID = 3335953140550341451L;

	public static final Font BAR_TEXT_FONT = new Font("Verdana", Font.BOLD, 12);

	private Color barColor;

	protected int value, maxValue;

	public ERAInfoBar(Color barColor) {
		super();

		this.value = 50;
		this.maxValue = 100;
		this.barColor = barColor;
	}

	public abstract void populate();

	public abstract String getBarString();

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2 = (Graphics2D) g.create();
		g2.setColor(new Color(50, 50, 50));
		g2.fillRoundRect(0, 0, this.getWidth() - 1, this.getHeight() - 1, 10,
				10);
		g2.setColor(barColor);
		g2.fillRoundRect(0, 0, (this.value * this.getWidth()) / this.maxValue,
				this.getHeight() - 1, 10, 10);
		g2.setColor(Color.BLACK);
		g2.drawRoundRect(0, 0, this.getWidth() - 1, this.getHeight() - 1, 10,
				10);
		Color textColor = new Color(barColor.getRed() / 2,
				barColor.getGreen() / 2, barColor.getBlue() / 2);
		g2.setColor(textColor);
		String text = this.getBarString();
		FontMetrics metrics = new FontMetrics(BAR_TEXT_FONT) {
		};
		Rectangle2D bounds = metrics.getStringBounds(text, null);
		int widthInPixels = (int) bounds.getWidth();
		g2.setFont(BAR_TEXT_FONT);
		g2.drawString(text, (this.getWidth() / 2) - (widthInPixels / 2), 13);
		g2.dispose();
	}

}
